package com.example.demo.impl;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.api.IVideoService;
import com.example.demo.define.UserDefine;
import com.example.demo.utils.HttpClient;
import com.example.demo.utils.HttpUitl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author xumf
 * @date 2019/9/29 19:21
 */
@Service
public class DahuaVideoService implements IVideoService {

    @Autowired
    private RestTemplate restTemplate;


    @Override
    public String sendFirstAuthorize() {
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("userName",UserDefine.USER_VALUE);
        jsonObject.put("clientType",UserDefine.CLIENT_TYPE);
        try {
            String result=HttpClient.postByJson(POST_AUTHORIZE_URL,jsonObject.toString());
            return result;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String sendSecondAuthorize(String signature,String randomKey) {
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("userName",UserDefine.USER_VALUE);
        jsonObject.put("signature",signature);
        jsonObject.put("randomKey",randomKey);
        jsonObject.put("encryptType","MD5");
        jsonObject.put("clientType",UserDefine.CLIENT_TYPE);
        try {
            String result=HttpClient.postByJson(POST_AUTHORIZE_URL,jsonObject.toString());
            return result;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String  keepAlive(String token) {
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("token",token);
        try {
            String result= HttpClient.putByJson(KEEP_ALIVE_URL,token,jsonObject.toString());
            System.out.println(result);
            return result;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String getVideoDeviceTree(String id, Integer noteType, String typeCode, Integer page, Integer pageSize) {
        Map<String,Object> parameterMap=new HashMap<>();
        parameterMap.put("id",id);
        parameterMap.put("noteType",noteType);
        parameterMap.put("typeCode",typeCode);
        parameterMap.put("page",page);
        parameterMap.put("pageSize",pageSize);
        String parameter=mapToString(parameterMap);
        String result=HttpUitl.sendGet(GET_DEVICE_TREE,parameter);
        return result;
    }

    @Override
    public String getRealMonitor(String channelId, String token) {
//        Map<String,Object> param = new HashMap<>();
//        param.put("channelId", channelId);
//        param.put("scheme", "HLS");
//        HttpHeaders headers = new HttpHeaders();
//        headers.add("X-Subject-Token", token);
//        ResponseEntity<String> response = restTemplate.exchange(
//                GET_VIDEO_URI+"?channelId={channelId}&scheme={scheme}",
//                HttpMethod.GET,
//                new HttpEntity<String>(headers),
//                String.class,
//                param);
//        return response.getBody();
        String url=GET_VIDEO_URI+"?channelId="+channelId+"&scheme=HLS";
        System.out.println(url);
        try {
            return   HttpClient.getByParam(url,token);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String mapToString(Map<String,Object> parameterMap){
        String param="";
        Set<String> ps=parameterMap.keySet();
        for(String p:ps){
            if(param.length()>0){
                param+="&";
            }
            param+=p+"="+parameterMap.get(p);
        }
        return param;
    }
}
